clear all
set memo 100m
capture log close

local filename Dynamic

log using Dynamic, replace text

local output_dir draft
local output_tex1 `filename'.tex

cd "C:\Users\rujia\Dropbox\Replication\treatyports"

use data/TreatyPorts_BasePanel.dta, clear



qui tabu id, gen(idummy)
qui tabu year, gen(ydummy)


tsset id  year
bysort id: gen rank=_n
order rank
by id: gen AnnualGrowth=(hpopu[2]-hpopu[1])/hpopu[1]/44 if rank<=2
order AnnualGrowth
by id: replace AnnualGrowth=100*(hpopu[3]-hpopu[2])/hpopu[2]/31 if rank==3
by id: replace AnnualGrowth=100*(hpopu[4]-hpopu[3])/hpopu[3]/29 if rank==4
by id: replace AnnualGrowth=100*(hpopu[5]-hpopu[4])/hpopu[4]/30 if rank==5
by id: replace AnnualGrowth=100*(hpopu[6]-hpopu[5])/hpopu[5]/43 if rank==6
by id: replace AnnualGrowth=100*(hpopu[7]-hpopu[6])/hpopu[6]/11 if rank==7
by id: replace AnnualGrowth=100*(hpopu[8]-hpopu[7])/hpopu[7]/18 if rank==8
by id: replace AnnualGrowth=100*(hpopu[9]-hpopu[8])/hpopu[8]/8 if rank==9
by id: replace AnnualGrowth=100*(hpopu[10]-hpopu[9])/hpopu[9]/10 if rank==10

sum AnnualGrowth, de
gen post_cmc=(year>cmcyear)

replace coastal2=0 if coastal2==.
replace yangtze2=0 if yangtze2==.
forvalues x=1(1) 10 {
gen latitude_`x'=latitude*ydummy`x'
gen longitude_`x'=longitude*ydummy`x'
gen landtax_`x'=landtax*ydummy`x'
gen silk_`x'=silk*ydummy`x'
gen tea_`x'=tea*ydummy`x'
gen grandcanal_`x'=dis_yunhe*ydummy`x'
gen taipingr_`x'=taipingrrebellion*ydummy`x'

}

label var AnnualGrowth "Annual Growth"
label var post_cmc "Post CMC"
local fe "idummy* ydummy*"

local control_fe "latitude_* longitude_* silk_* tea_* grandcanal_* taipingr_* landtax_*"


gen period_3=1 if cmcyear>=1854 &cmcyear<=1880 & year==1776

replace period_3=1 if cmcyear>=1880 &cmcyear<=1910 & year==1820

replace period_3=0 if period_3==.


gen period_2=1 if cmcyear>=1854 &cmcyear<=1880 & year==1820

replace period_2=1 if cmcyear>=1880 &cmcyear<=1910 & year==1851

replace period_2=0 if period_2==.


gen period_1=1 if cmcyear>=1854 &cmcyear<=1880 & year==1851

replace period_1=1 if cmcyear>=1880 &cmcyear<=1910 & year==1880

replace period_1=0 if period_1==.



gen period0=1 if cmcyear>=1854 &cmcyear<=1880 & year==1880

replace period0=1 if cmcyear>=1880 &cmcyear<=1910 & year==1910

replace period0=0 if period0==.



gen period1=1 if cmcyear>=1854 &cmcyear<=1880 & year==1910

replace period1=1 if cmcyear>=1880 &cmcyear<=1910 & year==1953

replace period1=0 if period1==.


gen period2=1 if cmcyear>=1854 &cmcyear<=1880 & year>=1950

replace period2=1 if cmcyear>=1880 &cmcyear<=1910 & year>=1964

replace period2=0 if period2==.

label var period_3 "3 Periods Before"
label var period_2 "2 Periods Before"
label var period_1 "1 Period Before"
label var period0 "Treaty Period"
label var period1 "1 Period After"
label var period2 "2 and More Periods After"

#delimit ;
eststo clear;

eststo: regress AnnualGrowth period_3 period_2 period0 period1 period2 `fe', noconstant vce(cluster id);
estadd local fe "Y";
estadd scalar nobs=e(N);
estadd scalar rsqaure=e(r2);


eststo:  regress AnnualGrowth period_3 period_2 period0 period1 period2 `fe'  `control_fe', noconstant vce(cluster id);
estadd local fe "Y";
estadd local control_fe "Y";
estadd scalar rsqaure=e(r2);
estadd scalar nobs=e(N);

eststo:  regress AnnualGrowth period_3 period_2 period0 period1 period2 `fe'  `control_fe' if wave!=1, noconstant vce(cluster id);
estadd local fe "Y";
estadd local control_fe "Y";
estadd scalar rsqaure=e(r2);
estadd scalar nobs=e(N);


esttab using draft/`output_tex1', replace
	width(\hsize) 
	mlabel (, depvar)
	b(3)
	se(3)
	noconstant
	star(* 0.10 ** 0.05 *** 0.01)
	keep(
period_3
period_2
period0
period1
period2
		)
		order(
		period_3
period_2
period0
period1
period2
)
		
	label
	stats(
		fe
		control_fe
		 nobs rsquare
		, 
		labels(
			"Prefecture and Year FE" 
			"Controls*Year FE" 
		
			"\# observations"
		 "R-squared"
			) 
		fmt(0)
		layout(@)
		)
	nonotes
	;
